package com.whq.thrift.api.web.admin.controller.bs.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.whq.common.StringUtil;
import com.whq.thrift.api.web.admin.db.config.Config;
import com.whq.thrift.api.web.admin.service.config.ConfigService;
import com.whq.thrift.api.web.admin.util.PageData;

@Controller
@Scope("prototype")
public class ConfigController {
	private static final int DEFAULT_PAGE_SIZE = 25;
	@Autowired
	private ConfigService configService;

	@RequestMapping(value="bs/config/config/list")
	public String list(@RequestParam(value = "conf_key", required = false) String conf_key,
			@RequestParam(value = "currentPage", required = true, defaultValue = "1") Integer currentPage,
			@RequestParam(value = "pageSize", required = false) Integer pageSize, 
			Model model) {
		PageData<Config> pageData = new PageData<Config>();
		pageData.setCurrentPage(currentPage);
		if ((pageSize == null) || (pageSize <= 0)) {
			pageSize = DEFAULT_PAGE_SIZE;
		}
		pageData.setPageSize(pageSize);

		// 查询参数
		Map<String, Object> whereParams = new HashMap<String, Object>(0);
		if (StringUtil.isNotBlank(conf_key)) {
			whereParams.put("conf_key", conf_key);
		}

		int count = this.configService.findRecordCount(whereParams);
		pageData.setTotalCount(count);

		int beginIndex = (currentPage - 1) * pageSize;
		List<Config> resultList = this.configService.findRecordsByMap(whereParams, beginIndex, pageSize);
		pageData.setResultList(resultList);

		model.addAttribute("conf_key", conf_key);
		model.addAttribute("pageData", pageData);

		return "bs/config/config/list";
	}

	@RequestMapping(value = "bs/config/config/show")
	public String show(@RequestParam(value = "id", required = true) String id, Model model) {
		if (id == null) {
			return "error";
		}
		Config record = this.configService.findRecordByKey(id);
		model.addAttribute("record", record);
		if (record == null) {
			return "error";
		}
		return "bs/config/config/show";
	}
	
	@RequestMapping(value="bs/config/config/showAdd")
	public String showAdd(Model model){
		Config record = new Config();
		model.addAttribute("record", record);
		return "bs/config/config/showAdd";
	}
	
	@RequestMapping(value="bs/config/config/add")
	public String add(@Valid Config record, 
			BindingResult result, 
			Model model) {
		Config existRecord = this.configService.findRecordByKey(record.getConf_key());
		if (existRecord != null) {
			model.addAttribute("errMsg", "conf_key已存在，不能重复！！");
			model.addAttribute("record", record);
			return "bs/config/config/showAdd";
		}
		
		this.configService.addRecord(record);
		return "redirect:show.do?id=" + record.getConf_key();
	}
	
	@RequestMapping(value="bs/config/config/showUpd")
	public String showUpd(@RequestParam(value="id", required=true) String id, 
			Model model){
		if (id == null) {

			return "error";
		}
		Config record = this.configService.findRecordByKey(id);
		if (record == null) {
			return "error";
		}
		model.addAttribute("record", record);
		return "bs/config/config/showUpd";
	}
	
	@RequestMapping(value="bs/config/config/upd")
	public String upd(@Valid Config record, 
			BindingResult result, Model model) {
		if (result.hasErrors()) {
			model.addAttribute("record", record);
			return "bs/config/config/showUpd";
		}
		
		this.configService.updateRecord(record);
		return "redirect:show.do?id=" + record.getConf_key();
	}
	
	@RequestMapping(value="bs/config/config/del")
    public String del(@RequestParam(value = "id", required = true, defaultValue = "0") String id,
            Model model) {
        this.configService.deleteRecord(id);
        return "bs/config/config/delOk";
    }
	
	@RequestMapping(value="bs/config/config/delRecords")
    public String delRecords(String[] delIds,
            Model model) {
        if (delIds != null) {
            for (int i = 0; i < delIds.length; i++) {
                if (delIds[i] != null) {
                    this.configService.deleteRecord(delIds[i]);
                }
            }
        }
        return "redirect:list.do";
    }
}
